<template>
  <j-modal
    :confirmLoading="confirmLoading"
    :dialogStyle="dialogStyle"
    :title="title"
    :visible="visible"
    :width="700"
    cancelText="关闭"
    switchFullscreen
    @cancel="handleCancel"
    @ok="handleOk">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="地区">
          <a-select v-decorator="['areaName', {
            rules: [
              {
                required: true,
                message: '地区不能为空!',
              },
            ],
            }]" placeholder="请选择地区" @change="handleChange">
            <a-select-option value="全市">全市</a-select-option>
            <a-select-option value="黄浦区">黄浦区</a-select-option>
            <a-select-option value="徐汇区">徐汇区</a-select-option>
            <a-select-option value="长宁区">长宁区</a-select-option>
            <a-select-option value="静安区">静安区</a-select-option>
            <a-select-option value="普陀区">普陀区</a-select-option>
            <a-select-option value="虹口区">虹口区</a-select-option>
            <a-select-option value="杨浦区">杨浦区</a-select-option>
            <a-select-option value="闵行区">闵行区</a-select-option>
            <a-select-option value="宝山区">宝山区</a-select-option>
            <a-select-option value="嘉定区">嘉定区</a-select-option>
            <a-select-option value="浦东新区">浦东新区</a-select-option>
            <a-select-option value="金山区">金山区</a-select-option>
            <a-select-option value="松江区">松江区</a-select-option>
            <a-select-option value="青浦区">青浦区</a-select-option>
            <a-select-option value="奉贤区">奉贤区</a-select-option>
            <a-select-option value="崇明区">崇明区</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="年份">
          <a-select v-decorator="['year', {
            rules: [
              {
                required: true,
                message: '年份不能为空!',
              },
            ],
            }]" placeholder="请选择年份">
            <a-select-option v-for="(i,index) in yearList" :key="index" :value="i.key">{{ i.name }}
            </a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="能源消费总量">
          <a-input v-decorator="['totalEnergyConsumption', {
            rules: [
              {
                required: true,
                message: '仅能输入数字和两位小数，不能为空或为负数！',
                pattern: /^[0-9]+\.?[0-9]{0,2}$/
              },
            ],
          }]" placeholder="请输入能源消费总量" suffix="tce" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="预测能源消费总量">
          <a-input v-decorator="['totalEnergyConsumptionYc', {
            rules: [
              {
                required: true,
                message: '仅能输入数字和两位小数，不能为空或为负数！',
                pattern: /^[0-9]+\.?[0-9]{0,2}$/
              },
            ],
          }]" placeholder="请输入预测能源消费总量" suffix="tce" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="碳排放总量">
          <a-input v-decorator="['totalCarbonEmissions', {
            rules: [
              {
                required: true,
                message: '仅能输入数字和两位小数，不能为空或为负数！',
                pattern: /^[0-9]+\.?[0-9]{0,2}$/
              },
            ],
          }]" placeholder="请输入碳排放总量" suffix="tco2" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="预测碳排放总量">
          <a-input v-decorator="['totalCarbonEmissionsYc', {
            rules: [
              {
                required: true,
                message: '仅能输入数字和两位小数，不能为空或为负数！',
                pattern: /^[0-9]+\.?[0-9]{0,2}$/
              },
            ],
          }]" placeholder="请输入预测碳排放总量" suffix="tco2" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="能耗强度">
          <a-input v-decorator="['energyConsumptionIntensity', {
            rules: [
              {
                required: false,
                message: '仅能输入数字和两位小数，不能为空或为负数！',
                pattern: /^[0-9]+\.?[0-9]{0,2}$/
              },
            ],
          }]" placeholder="请输入能耗强度" suffix="tce/万元" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="碳排放强度">
          <a-input v-decorator="['carbonEmissionIntensity', {
            rules: [
              {
                required: false,
                message: '仅能输入数字和两位小数，不能为空或为负数！',
                pattern: /^[0-9]+\.?[0-9]{0,2}$/
              },
            ],
          }]" placeholder="请输入碳排放强度" suffix="tco2/万元" />
        </a-form-item>
        <a-form-item
          v-if="showMarkInput"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="分数">
          <a-input v-decorator="['mark', {
            rules: [
              {
                required: true,
                message: '仅能输入小于等于100的数（可为两位小数），不能为空或为负数！',
                pattern: /^(?:100(?:\.00?)?|\d{1,2}(?:\.\d{1,2})?)$/
              },
            ],
          }]" placeholder="请输入分数" suffix="分" />
        </a-form-item>
      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>
import pick from 'lodash.pick'
import { httpAction } from '@api/manage'

const date = new Date()
const count = date.getFullYear() - 2020
const yearlist = []
for (let i = 0; i < count + 1; i++) {
  yearlist.push({
    name: date.getFullYear() - i,
    key: date.getFullYear() - i
  })
}
export default {
  name: 'mapDataModal',
  data() {
    return {
      showMarkInput: false,
      selectedArea: '',
      confirmLoading: false,
      title: '',
      visible: false,
      form: this.$form.createForm(this),
      yearList: yearlist,
      currentTime: '',
      dialogStyle: {
        top: '300px'
        // right:"20px",
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      validatorRules: {
        totalEnergyConsumption: {
          rules: [{ required: true, trigger: 'change', message: '请输入数字！', pattern: '^\d{1,}\.\d{1,}|\d{1,}]/g' }]
        }
      },
      url: {
        add: '/carbon/carbonOverviewYearData/add',
        edit: '/carbon/carbonOverviewYearData/edit'
      }
    }
  },
  watch: {
    // 设置一个watch来监听 selectedArea 变化
    selectedArea(newVal, oldVal) {
      if (newVal !== '全市') {
        this.showMarkInput = true
      } else {
        this.showMarkInput = false
      }
    }
  },
  mounted() {
    this.selectedArea = this.form.getFieldValue('areaName')

  },
  methods: {
    add() {
      this.edit({})
    },
    edit(record) {
      console.log(record)
      this.visible = true
      this.form.resetFields()
      this.model = Object.assign({}, record)
      this.$nextTick(() => {
        this.form.setFieldsValue(pick(
          this.model,
          'areaName',
          'totalEnergyConsumption',
          'totalCarbonEmissions',
          'totalEnergyConsumptionYc',
          'totalCarbonEmissionsYc',
          'energyConsumptionIntensity',
          'carbonEmissionIntensity',
          'mark',
          'year'
        ))
        //   this.model.areaName去除空格后为全市，那么就显示分数输入框
        if (this.model.areaName.trim() === '全市') {
          this.showMarkInput = false
        } else {
          this.showMarkInput = true
        }
      })
    },
    handleChange(value) {
      // 更新selectedArea每次地区选择发生变化时
      this.selectedArea = value
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleCancel() {
      this.close()
    },
    handleOk() {
      let that = this
      this.form.validateFields((err, values) => {
        if (!err) {
          let httpurl = ''
          let method = ''
          this.confirmLoading = true
          console.log(this.model)
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'post'
          }
          console.log(httpurl)
          let formData = Object.assign(this.model, values)
          console.log(formData)
          httpAction(httpurl, formData, method).then(res => {
            if (res.success) {
              that.$message.success(res.message)
              that.$emit('refresh')
            } else {
              that.$message.error(res.message)
            }
          }).finally(() => {
            that.confirmLoading = false
            that.close()
            that.$parent.$parent.loadData()
          })
        }
      })
    }
  }
}
</script>

<style scoped>

</style>